<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>欢迎页</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 数据统计 -->
    <el-row :gutter="10">
      <el-col :span="6">
        <div class="statistics" style="background: #409eff">
          <div class="title">
            <p>待出票统计</p>
          </div>
          <div class="num">{{ tOrderCnt }}</div>
          <div class="tip">待出票订单</div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="statistics" style="background: #e6a23c">
          <div class="title">
            <p>订单统计</p>
          </div>
          <div class="num">{{ todayOrderCnt }}</div>
          <div class="tip">今日订单数量</div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="statistics" style="background: #67c23a">
          <div class="title">
            <p>订单统计</p>
          </div>
          <div class="num">{{ monthOrderCnt }}</div>
          <div class="tip">本月订单数量</div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="statistics" style="background: #909399">
          <div class="title">
            <p>用户统计</p>
          </div>
          <div class="num">{{ memberCnt }}</div>
          <div class="tip">总用户数量</div>
        </div>
      </el-col>
    </el-row>

    <!-- Echarts图表 -->
    <div class="echarts-container">
      <el-card id="chart1"></el-card>
      <el-card id="chart2"></el-card>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
import Api from "@/api/Api";

export default {
  data() {
    return {
      tOrderCnt: 0,
      todayOrderCnt: 0,
      monthOrderCnt: 0,
      memberCnt: 0,
      orderList: [],
      orderDateList: [], //横坐标
      orderCntList: [], //数据
      orderAmountList: [],
      myChartStyle: { float: "left", width: "100%", height: "100%"} //图表样式
    }
  },
  created() {
    this.welcomeData()
  },
  methods: {
    welcomeData(){
      Api.welcomeData({}).then(res =>{
        if (res.success) {
          this.todayOrderCnt = res.data.todayOrderCnt;
          this.monthOrderCnt = res.data.monthOrderCnt;
          this.memberCnt = res.data.memberCnt;
          this.tOrderCnt = res.data.tOrderCnt;
          this.orderDateList = res.data.orderDateList;
          this.orderCntList = res.data.orderCntList;
          this.orderAmountList = res.data.orderAmountList;
          this.drawLine('chart1');
          this.initBar();
        } else {
          return this.$message.error('获取数据失败')
        }
      });
    },
    drawLine(id) {
      this.charts = echarts.init(document.getElementById(id));
      this.charts.setOption({
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["近七日订单"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },

        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.orderDateList,
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            name: "近七日订单",
            type: "line",
            stack: "总量",
            data: this.orderCntList,
          },
        ],
      });
    },

    initBar() {
      // 基本柱状图
      const option = {
        xAxis: {
          data: this.orderDateList
        },
        yAxis: {},
        series: [
          {
            type: "bar", //形状为柱状图
            data: this.orderAmountList,
            itemStyle:{
              color:'#40B9FF'
            }
          }
        ],
        title:{
          show:true,
          text:"近7日交易额"
        }
      };
      const myChart = echarts.init(document.getElementById("chart2"));
      myChart.setOption(option);
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    }
  }
}
</script>

<style lang="less" scoped>
.el-row {
  padding: 0 9px;
  .statistics {
    color: white;
    height: 100px;
    border-radius: 4px;
    padding: 0 8px;
    .title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      p {
        font-size: 13px;
        font-weight: bold;
      }
    }
    .num {
      font-size: 20px;
      font-weight: bold;
      text-align: center;
    }
    .tip {
      font-size: 12px;
    }
  }
}
.echarts-container {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  .el-card {
    height: 400px;
    width: 50%;
    margin: 0 8px;
    padding: 20px;
  }
}
</style>